package com.wangdao.wechatmall.service.wx;

import com.sun.org.apache.xml.internal.resolver.CatalogManager;
import com.wangdao.wechatmall.bean.pojo.Category;
import com.wangdao.wechatmall.bean.pojo.CategoryExample;
import com.wangdao.wechatmall.bean.vo.wx.CatalogIndexVO;
import com.wangdao.wechatmall.bean.vo.wx.CurrentCatalogVO;
import com.wangdao.wechatmall.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: JK
 * @Date: create in 2021/7/11 23:43
 */
@Service
public class WXCatalogServiceImpl implements WXCatalogService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public CatalogIndexVO showCatalog() {
        // 1.查询父目录
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andPidEqualTo(0);
        categoryExampleCriteria.andDeletedEqualTo(0);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);

        Category currentCategory = categoryList.get(0);

        // 2.查询子目录
        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria categoryExample1Criteria = categoryExample1.createCriteria();
        categoryExample1Criteria.andPidEqualTo(currentCategory.getId());
        categoryExample1Criteria.andDeletedEqualTo(0);
        List<Category> currentSubCategory = categoryMapper.selectByExample(categoryExample1);

        CatalogIndexVO catalogIndexVO = new CatalogIndexVO();
        catalogIndexVO.setCurrentCategory(currentCategory);
        catalogIndexVO.setCategoryList(categoryList);
        catalogIndexVO.setCurrentSubCategory(currentSubCategory);

        return catalogIndexVO;
    }

    @Override
    public CurrentCatalogVO showCurrentCatalog(Integer id) {
        // 1.根据id查询当前父目录
        Category currentCategory = categoryMapper.selectByPrimaryKey(id);

        // 2.查询子目录
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(currentCategory.getId());
        criteria.andDeletedEqualTo(0);
        List<Category> currentSubCategory = categoryMapper.selectByExample(categoryExample);

        CurrentCatalogVO currentCatalogVO = new CurrentCatalogVO();
        currentCatalogVO.setCurrentCategory(currentCategory);
        currentCatalogVO.setCurrentSubCategory(currentSubCategory);

        return currentCatalogVO;
    }
}
